<?php
/**
 * Created by PhpStorm.
 * User: 38339
 * Date: 2021-05-26
 * Time: 14:58
 */

namespace app\common\business\table;
use app\common\model\table\CommonTableConfig as Model;
use think\facade\Db;

class CommonTableConfig
{

    /**
     * 【方法注释】获取单条数据
     * @param $where 查询条件数组
     * @return array|null|\think\Model 返回单个对象
     */
    public static function getOne($where,$isHandle=false){
        return Model::getOne($where,$isHandle);
    }

    /**
     * 【方法注释】获取数据
     * @param $where 查询条件数组
     * @param $limit_start 第几页
     * @param $page_size 取多少条
     * @return array 返回数据列表
     */
   public static function getList($where,$limit_start,$page_size){
       return Model::getList($where,$limit_start,$page_size);
   }

    /**
     * 【方法注释】获取数据
     * @param $where 查询条件数组
     * @return array 返回数据列表
     */
    public static function getListAll($where){
        return Model::getListAll($where);
    }

    /**
     * 【方法注释】获取数据条数
     * @param $where 查询条件数组
     * @return int 返回符合条件的数据条数
     */
   public static function getCount($where){
       return getAllPage(Model::getCount($where));
   }

    /**
     * 【方法注释】新增数据
     * @param $receiveData 数据数组
     * @param $admin 操作人对象
     * @return bool 返回是否成功
     */
   public static function insert($receiveData,$admin_name = ''){
       $data = [
           'table_name' => $receiveData['table_name'],
           'remark' => $receiveData['remark'],
           'create_time' => date('Y-m-d H:i:s'),
           'status' => $receiveData['status'],
           'update_time' => date('Y-m-d H:i:s'),
           'table_namespace_id' => $receiveData['table_namespace_id'],
           'admin_name' => $admin_name
       ];
       Model::insert($data);
       return true;
   }

    /**
     * 【方法注释】修改数据
     * @param $receiveData 数据数组
     * @return bool 返回是否成功
     */
   public static function edit($receiveData){
       $data = [
           'table_name' => $receiveData['table_name'],
           'remark' => $receiveData['remark'],
           'status' => $receiveData['status'],
           'update_time' => date('Y-m-d H:i:s'),
           'table_namespace_id' => $receiveData['table_namespace_id']
       ];
       Model::where(['id'=>$receiveData['id']])->update($data);
       return true;
   }

    /**
     * 【方法注释】更新数据库字段
     * @param $field 要修改的字段
     * @param $value 结果值
     * @param $load 源数据对象
     * @return bool
     */
    public static function updateStatus($field,$value,$load){
        Model::where(['id'=>$load['id']])->update([
            $field => $value,
            'update_time' => date('Y-m-d H:i:s')
        ]);
        return true;
    }

    /*
     * 获取需要查询的表的列信息等
     */
    public static function selectColumns($model){
        $table_name = $model['table_name'];
        $table_schema = env('database.database');
        $sql = "SELECT
                    COLUMN_NAME AS 'column',
                    IS_NULLABLE AS 'is_null',
                    COLUMN_DEFAULT AS 'default_value',
                    DATA_TYPE AS 'data_type',
                    COLUMN_COMMENT AS 'column_remark'
                FROM
                    information_schema.`COLUMNS`
                WHERE
                    table_name = '$table_name' AND table_schema = '$table_schema'";
        $result = Db::query($sql);
        if(count($result) > 0){
            $number = 0;
            foreach ($result as $item){
                if(in_array($item['column'],['id','status','create_time','update_time','remark','admin_name'])){
                    $number ++;
                }
            }
            if($number < 6){
                jsonFail('数据表【'.$table_name.'】定义基础字段不完善，请完善数据表');
            }
        }else{
            jsonFail('该数据表不存在字段');
        }
        return $result;
    }

    /** delDir()删除文件夹及文件夹内文件函数
     * @param string $path 文件夹路径
     * @param string $delDir 是否删除改
     * @return boolean
     */
    public static function delDir($path, $del = false){
        if(!file_exists($path)){
            return true;
        }
        $handle = opendir($path);
        if ($handle) {
            while (false !== ($item = readdir($handle))) {
                if (($item != ".") && ($item != "..")) {
                    is_dir("$path/$item") ? self::delDir("$path/$item", $del) : unlink("$path/$item");
                }
            }
            closedir($handle);

            if ($del) {
                return rmdir($path);
            }
        }elseif (file_exists($path)) {
            return unlink($path);
        }else {
            return false;
        }
    }

    /**
     * 【方法注释】生成model
     * @param $model
     */
    public static function generateModel($model,$namespace,$columnList){
        $modelNamespaceArr = explode('\\',$namespace['model_namespace']);
        $module_name = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $table_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/model/".$module_name;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $table_name.'.php';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        $file = fopen($file_path,'w');
        //这里打开和生成的文件，现在开始执行写入文件
        fwrite($file,'<?php'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,'namespace '.$namespace['model_namespace'].';'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,'use think\Model;'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"class $table_name extends Model \r\n");
        fwrite($file,"{ \r\n");
        fwrite($file,'    protected $name = '."'".self::cutTablePrefix($model['table_name'],$namespace['table_prefix'])."'; \r\n");
        fwrite($file,"\r\n");

        //$schema属性开始
        fwrite($file,'    protected $schema = ['. "\r\n");
        foreach ($columnList as $col){
            $col_column = $col['column'];
            $col_data_type = $col['data_type'];
            $col_column_remakr = $col['column_remark'];
            fwrite($file,'        '."'$col_column'".'             => '."'$col_data_type', //$col_column_remakr". "\r\n");
        }
        fwrite($file,'    ];'. "\r\n");
        //$schema属性结束

        fwrite($file,"\r\n");
        fwrite($file,"    //处理数据模型 \r\n");
        fwrite($file,"    public static function fillModels(".'$models'."){ \r\n");
        fwrite($file,"        ".'$list'." = []; \r\n");
        fwrite($file,"        foreach (".'$models'." as ".'$model'.") { \r\n");
        fwrite($file,"            ".'$listItem'." = static::fillModel(".'$model'."); \r\n");
        fwrite($file,"            ".'$list'."[] = ".'$listItem'."; \r\n");
        fwrite($file,"        } \r\n");
        fwrite($file,"        return ".'$list'."; \r\n");
        fwrite($file,"    } \r\n");
        fwrite($file,"\r\n");
        fwrite($file,"    public static function fillModel(".'$model'."){ \r\n");
        fwrite($file,'        $data = [];'."\r\n");
        $isHandleFlag = false;
        foreach ($columnList as $col){
            if($col['column'] == 'status'){
                fwrite($file,'        $data['."'".$col['column']."'".'] = (string)$model['."'".$col['column']."'".'];'."\r\n");
            }else{
                if(strpos($col['column_remark'],'[富文本]') !== false){
                    fwrite($file,'        $data['."'".$col['column']."'".'] = returnEditor($model['."'".$col['column']."'".']);'."\r\n");
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    fwrite($file,'        $data['."'".$col['column']."'".'] = fullurl($model['."'".$col['column']."'".']);'."\r\n");
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false){
                    fwrite($file,'        $data['."'".$col['column']."'".'] = fullurlList($model['."'".$col['column']."'".']);'."\r\n");
                }elseif(strpos($col['column_remark'],'[radio]') !== false){
                    fwrite($file,'        $data['."'".$col['column']."'".'] = (string)$model['."'".$col['column']."'".'];'."\r\n");
                    $func_name = 'get'.self::getTableFileName($col['column']).'Name';
                    fwrite($file,'        $data['."'".$col['column']."_name'".'] = self::'.$func_name.'($model['."'".$col['column']."'".']);'."\r\n");
                    $isHandleFlag = true;
                }elseif(strpos($col['column_remark'],'[select]') !== false){
                    fwrite($file,'        $data['."'".$col['column']."'".'] = (string)$model['."'".$col['column']."'".'];'."\r\n");
                    $func_name = 'get'.self::getTableFileName($col['column']).'Name';
                    fwrite($file,'        $data['."'".$col['column']."_name'".'] = self::'.$func_name.'($model['."'".$col['column']."'".']);'."\r\n");
                    $isHandleFlag = true;
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false){
                        fwrite($file,'        $data['."'".$col['column']."'".'] = $model['."'".$col['column']."'".'];'."\r\n");
                    }
                }
            }
        }
        fwrite($file,'        return $data;'."\r\n");
        fwrite($file,"    } \r\n");
        fwrite($file,"\r\n");

        if($isHandleFlag){
            foreach ($columnList as $col){
                if(strpos($col['column_remark'],'[radio]') !== false || strpos($col['column_remark'],'[select]') !== false){
                    $func_name = 'get'.self::getTableFileName($col['column']).'Name';
                    fwrite($file,"    /** \r\n");
                    fwrite($file,"     *【方法注释】处理".$col['column_remark']. "\r\n");
                    fwrite($file,'     *@param $value 传入的值'. "\r\n");
                    fwrite($file,'     *@param string 返回的值'. "\r\n");
                    fwrite($file,'     */'. "\r\n");
                    fwrite($file,'    public static function '.$func_name.'($value){'."\r\n");
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $pattern_str = trim(str_replace('】','',str_replace('【','',$pattern_str)));
                    if(!empty($pattern_str)){
                        $pattern_arr = explode('|',$pattern_str);
                        if(count($pattern_arr) > 0){
                            fwrite($file,'        $arr = ['."\r\n");
                            foreach ($pattern_arr as $pattern){
                                if(!empty($pattern)){
                                    $item_arr = explode('-',$pattern);
                                    if(count($item_arr) > 1) {
                                        fwrite($file, '            '.$item_arr[0].'=>"'.$item_arr[1].'",' . "\r\n");
                                    }else {
                                        fwrite($file, '            '.$item_arr[0].'=>"",' . "\r\n");
                                    }
                                }
                            }
                            fwrite($file,'        ];'."\r\n");
                            fwrite($file,'        return $arr[$value];'."\r\n");
                        }else{
                            fwrite($file,'        return "";'."\r\n");
                        }
                    }
                    fwrite($file,"    } \r\n");
                    fwrite($file,"\r\n");
                }
            }
        }

        //获取数据列表查询开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】获取数据列表 \r\n");
        fwrite($file,'     *@param $where 查询的条件数组'. "\r\n");
        fwrite($file,'     *@param $limit_start 获取数据，从第$limit_start条数据开始'. "\r\n");
        fwrite($file,'     *@param $page_size 获取数据，从第$limit_start条数据开始，取$page_size条数据'. "\r\n");
        fwrite($file,'     *@param array 返回的数据，数组'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function getList($where, $limit_start, $page_size){'."\r\n");
        fwrite($file,'        $lists =  self::where($where)->order('."'id','desc'".')->limit($limit_start,$page_size)->select();'."\r\n");
        fwrite($file,'        return self::fillModels($lists);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取数据列表查询结束

        fwrite($file,"\r\n");

        //获取数据条数查询开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】获取数据条数 \r\n");
        fwrite($file,'     *@param $where 查询的条件数组'. "\r\n");
        fwrite($file,'     *@param int 返回的数据条数'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function getCount($where){'."\r\n");
        fwrite($file,'        return self::where($where)->count();'."\r\n");
        fwrite($file,"    } \r\n");
        //获取数据条数查询结束

        fwrite($file,"\r\n");

        //获取单条数据查询开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】获取单条数据 \r\n");
        fwrite($file,'     *@param $where 查询的条件数组'. "\r\n");
        fwrite($file,'     *@param array 返回的单条数据'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function getOne($where){'."\r\n");
        fwrite($file,'        return self::fillModel(self::where($where)->find());'."\r\n");
        fwrite($file,"    } \r\n");
        //获取单条数据查询结束

        fwrite($file,"\r\n");
        fwrite($file,"} \r\n");
        fclose($file);
        return true;
    }

    /**
     * 【方法注释】生成validate 验证model
     * @param $model
     */
    public static function generateValidateModel($model,$namespace,$columnList){
        $modelNamespaceArr = explode('\\',$namespace['validate_namespace']);
        $module_name = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $table_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/validate/".$module_name;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $table_name.'.php';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        $file = fopen($file_path,'w');
        //这里打开和生成的文件，现在开始执行写入文件
        fwrite($file,"<?php \r\n");
        fwrite($file,"\r\n");
        fwrite($file,'namespace '.$namespace['validate_namespace'].';'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,'use think\Validate;'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"class $table_name extends Validate \r\n");
        fwrite($file,"{ \r\n");

        //validate  rule验证规则设置开始
        fwrite($file,'    protected $rule = ['. "\r\n");
        foreach ($columnList as $col){
            if($col['is_null'] == 'NO' && is_null($col['default_value']) && !in_array($col['column'],['id','status','admin_name'])){
                fwrite($file,"        '".$col['column']."' => 'require',"."\r\n");
            }
        }
        fwrite($file,"    ];\r\n");
        fwrite($file,"\r\n");
        //validate  rule验证规则设置结束

        //validate  message验证规则提示设置开始
        fwrite($file,'    protected $message = ['. "\r\n");
        foreach ($columnList as $col){
            if($col['is_null'] == 'NO' && is_null($col['default_value']) && !in_array($col['column'],['id','status','admin_name'])){
                if(strpos($col['column_remark'],'[富文本]') !== false){
                    $column_remark = trim(str_replace('[富文本]','',$col['column_remark'])).'不能为空';
                    fwrite($file,"        '".$col['column'].".require' => '".$column_remark."',"."\r\n");
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    $column_remark = '请上传'.trim(str_replace('[单图上传]','',$col['column_remark']));
                    fwrite($file,"        '".$col['column'].".require' => '".$column_remark."',"."\r\n");
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false){
                    $column_remark = '请上传'.trim(str_replace('[多图上传]','',$col['column_remark']));
                    fwrite($file,"        '".$col['column'].".require' => '".$column_remark."',"."\r\n");
                }elseif(strpos($col['column_remark'],'[radio]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = '请选择'.trim(str_replace($pattern_str,'',str_replace('[radio]','',$col['column_remark'])));
                    fwrite($file,"        '".$col['column'].".require' => '".$column_remark."',"."\r\n");
                }elseif(strpos($col['column_remark'],'[select]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = '请选择'.trim(str_replace($pattern_str,'',str_replace('[select]','',$col['column_remark'])));
                    fwrite($file,"        '".$col['column'].".require' => '".$column_remark."',"."\r\n");
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false){
                        $column_remark = '请输入'.trim($col['column_remark']);
                        fwrite($file,"        '".$col['column'].".require' => '".$column_remark."',"."\r\n");
                    }
                }
            }
        }
        fwrite($file,"    ];\r\n");
        fwrite($file,"\r\n");
        //validate  message验证规则提示设置结束

        //validate  scene验证场景设置开始
        fwrite($file,'    protected $scene = ['. "\r\n");
        $addStr = '';
        $editStr = '';
        foreach ($columnList as $col){
            if($col['is_null'] == 'NO' && is_null($col['default_value']) && !in_array($col['column'],['id','status','admin_name'])){
                $addStr .= "'".$col['column']."',";
                $editStr .= "'".$col['column']."',";
            }
        }
        fwrite($file,"        'edit' => [".$editStr."'id'],". "\r\n");
        fwrite($file,"        'add' => [".rtrim($addStr,',')."],". "\r\n");
        fwrite($file,"    ];\r\n");
        //validate  scene验证场景设置结束

        fwrite($file,"\r\n");
        fwrite($file,"} \r\n");
        fclose($file);
        return true;
    }

    /**
     * 【方法注释】生成business 的业务层model
     * @param $model
     */
    public static function generateBusinessModel($model,$namespace,$columnList){
        $modelNamespaceArr = explode('\\',$namespace['business_namespace']);
        $module_name = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $table_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/business/".$module_name;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $table_name.'.php';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        $file = fopen($file_path,'w');
        //这里打开和生成的文件，现在开始执行写入文件
        fwrite($file,'<?php'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,'namespace '.$namespace['business_namespace'].';'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"use ".$namespace['model_namespace']."\\"."$table_name as Model;". "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"class $table_name \r\n");
        fwrite($file,"{ \r\n");

        //获取数据列表查询开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】获取数据列表 \r\n");
        fwrite($file,'     *@param $where 查询的条件数组'. "\r\n");
        fwrite($file,'     *@param $limit_start 获取数据，从第$limit_start条数据开始'. "\r\n");
        fwrite($file,'     *@param $page_size 获取数据，从第$limit_start条数据开始，取$page_size条数据'. "\r\n");
        fwrite($file,'     *@param array 返回的数据，数组'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function getList($where, $limit_start, $page_size){'."\r\n");
        fwrite($file,'        return Model::getList($where, $limit_start, $page_size);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取数据列表查询结束

        fwrite($file,"\r\n");

        //获取数据条数查询开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】获取数据条数 \r\n");
        fwrite($file,'     *@param $where 查询的条件数组'. "\r\n");
        fwrite($file,'     *@param int 返回的数据条数'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function getCount($where){'."\r\n");
        fwrite($file,'        return getAllPage(Model::getCount($where));'."\r\n");
        fwrite($file,"    } \r\n");
        //获取数据条数查询结束

        fwrite($file,"\r\n");

        //获取单条数据查询开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】获取单条数据 \r\n");
        fwrite($file,'     *@param $where 查询的条件数组'. "\r\n");
        fwrite($file,'     *@param array 返回的单条数据'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function getOne($where){'."\r\n");
        fwrite($file,'        return Model::getOne($where);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取单条数据查询结束

        fwrite($file,"\r\n");

        //新增数据开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】新增数据 \r\n");
        fwrite($file,'     *@param $receiveData 数组数组'. "\r\n");
        fwrite($file,'     *@param $admin_name 操作人'. "\r\n");
        fwrite($file,'     *@param $isFront 是否是前端添加'. "\r\n");
        fwrite($file,'     *@param bool 数据是否新增成功'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function insert($receiveData,$admin_name = '."''".',$isFront = false){'."\r\n");
        fwrite($file,'        $data = ['."\r\n");
        foreach ($columnList as $col){
            if(in_array($col['column'],['id','status','create_time','update_time','admin_name'])){
                switch ($col['column']){
                    case 'status':
                        fwrite($file,"             '".$col['column']."' => ".'$receiveData['."'".$col['column']."'],"."\r\n");
                        break;
                    case 'create_time':
                    case 'update_time':
                        fwrite($file,"             '".$col['column']."' => date('Y-m-d H:i:s'),"."\r\n");
                        break;
                    case 'admin_name':
                        fwrite($file,"             '".$col['column']."' => ".'$admin_name'.","."\r\n");
                        break;
                    default:
                        break;
                }
            }else{
                if(strpos($col['column_remark'],'[富文本]') !== false){
                    fwrite($file,"             '".$col['column']."' => ".'saveEditor($receiveData['."'".$col['column']."']),"."\r\n");
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    fwrite($file,"             '".$col['column']."' => ".'getImage($receiveData['."'".$col['column']."']),"."\r\n");
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false){
                    fwrite($file,"            '".$col['column']."' => ".'$isFront ? getImageListToStr1($receiveData['."'".$col['column']."']) :".' getImageListToStr1($receiveData['."'".$col['column']."'])," ."\r\n");
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false){
                        fwrite($file,"             '".$col['column']."' => ".'$receiveData['."'".$col['column']."'],"."\r\n");
                    }
                }
            }
        }
        fwrite($file,'        ];'."\r\n");
        fwrite($file,'        Model::insert($data);'."\r\n");
        fwrite($file,'        return true;'."\r\n");
        fwrite($file,"    } \r\n");
        //新增数据结束

        fwrite($file,"\r\n");

        //修改数据开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】修改数据 \r\n");
        fwrite($file,'     *@param $receiveData 数组数组'. "\r\n");
        fwrite($file,'     *@param $isFront 是否是前端添加'. "\r\n");
        fwrite($file,'     *@param bool 数据是否新增成功'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function edit($receiveData,$isFront = false){'."\r\n");
        fwrite($file,'        $data = ['."\r\n");
        foreach ($columnList as $col){
            if(in_array($col['column'],['id','status','create_time','update_time','admin_name'])){
                switch ($col['column']){
                    case 'status':
                        fwrite($file,"             '".$col['column']."' => ".'$receiveData['."'".$col['column']."'],"."\r\n");
                        break;
                    case 'update_time':
                        fwrite($file,"             '".$col['column']."' => date('Y-m-d H:i:s'),"."\r\n");
                        break;
                    default:
                        break;
                }
            }else{
                if(strpos($col['column_remark'],'[富文本]') !== false){
                    fwrite($file,"             '".$col['column']."' => ".'saveEditor($receiveData['."'".$col['column']."']),"."\r\n");
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    fwrite($file,"             '".$col['column']."' => ".'getImage($receiveData['."'".$col['column']."']),"."\r\n");
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false){
                    fwrite($file,"            '".$col['column']."' => ".'$isFront ? getImageListToStr1($receiveData['."'".$col['column']."']) :".' getImageListToStr1($receiveData['."'".$col['column']."'])," ."\r\n");
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false) {
                        fwrite($file, "             '" . $col['column'] . "' => " . '$receiveData[' . "'" . $col['column'] . "']," . "\r\n");
                    }
                }
            }
        }
        fwrite($file,'        ];'."\r\n");
        fwrite($file,'        Model::where(['."'id'=>".'$receiveData['."'id']])->update(".'$data);'."\r\n");
        fwrite($file,'        return true;'."\r\n");
        fwrite($file,"    } \r\n");
        //修改数据结束

        //更改状态开始
        fwrite($file,"    /** \r\n");
        fwrite($file,"     *【方法注释】更改状态 \r\n");
        fwrite($file,'     *@param $value 结果值'. "\r\n");
        fwrite($file,'     *@param $load 源数据'. "\r\n");
        fwrite($file,'     *@param bool 数据是否新增成功'. "\r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public static function updateStatus($value,$load){'."\r\n");
        fwrite($file,'        Model::where(['."'id'".'=>$load['."'id']])->update(["."\r\n");
        fwrite($file,'            '."'status'".' => $value,'."\r\n");
        fwrite($file,"            'update_time' => date('Y-m-d H:i:s')"."\r\n");
        fwrite($file,'        ]);'."\r\n");
        fwrite($file,'        return true;'."\r\n");
        fwrite($file,"    } \r\n");
        //更改状态结束

        fwrite($file,"\r\n");
        fwrite($file,"} \r\n");
        fclose($file);
        return true;
    }

    /**
     * 【方法注释】生成controller,增加controller方法的同事，还要将路由写入
     * @param $model
     */
    public static function generateControllerModel($model,$namespace,$columnList){
        $modelNamespaceArr = explode('\\',$namespace['admin_controller_namespace']);
        $module_name = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $table_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/admin_controller/".$module_name;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $table_name.'.php';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        $file = fopen($file_path,'w');
        //这里打开和生成的文件，现在开始执行写入文件
        fwrite($file,'<?php'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,'namespace '.$namespace['admin_controller_namespace'].';'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"use app\BaseController;". "\r\n");
        fwrite($file,"use ".$namespace['validate_namespace']."\\"."$table_name as ValidateModel;". "\r\n");
        fwrite($file,"use ".$namespace['business_namespace']."\\"."$table_name as serviceModel;". "\r\n");
        fwrite($file,"use think\\"."exception\ValidateException;". "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"class $table_name extends BaseController \r\n");
        fwrite($file,"{ \r\n");
        fwrite($file,"\r\n");
        //获取数据列表查询开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】获取数据列表 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function list(){'."\r\n");
        fwrite($file,'        $fildes = '."'page,status';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $where = [];'."\r\n");
        fwrite($file,'        if(!empty($receiveData['."'status'])){"."\r\n");
        fwrite($file,'            switch ($receiveData['."'status']){"."\r\n");
        fwrite($file,'                case 1:'."\r\n");
        fwrite($file,'                    $where[] = ['."'status','=',0];"."\r\n");
        fwrite($file,'                break;'."\r\n");
        fwrite($file,'                case 2:'."\r\n");
        fwrite($file,'                    $where[] = ['."'status','=',1];"."\r\n");
        fwrite($file,'                break;'."\r\n");
        fwrite($file,'            }'."\r\n");
        fwrite($file,'        }else{'."\r\n");
        fwrite($file,'            $where[] = ['."'status','in',[0,1]];"."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        $limit_start = ($receiveData['."'page']-1) * env('page_size');"."\r\n");
        fwrite($file,'        $data = ['."\r\n");
        fwrite($file,'            '."'lists'".' => serviceModel::getList($where,$limit_start,env('."'page_size')),"."\r\n");
        fwrite($file,'            '."'allpage'".' => serviceModel::getCount($where)'."\r\n");
        fwrite($file,'        ];'."\r\n");
        fwrite($file,'        jsonSuccess($data);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取数据列表查询结束

        fwrite($file,"\r\n");

        //修改和编辑开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】修改和编辑 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function edit(){'."\r\n");
        $filed_str = "";
        foreach ($columnList as $col){
            if(!in_array($col['column'],['create_time','update_time']) && strpos($col['column_remark'],'[标记字段]') === false){
                $filed_str .= $col['column'].',';
            }
        }
        $filed_str = rtrim($filed_str,',');
        fwrite($file,'        $fildes = '."'$filed_str';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $admin = session('."'admin_user'".');'."\r\n");
        fwrite($file,'        try{'."\r\n");
        fwrite($file,'            if(!empty($receiveData['."'id'])){"."\r\n");
        fwrite($file,'                validate(validateModel::class)->scene('."'edit'".')->check($receiveData);'."\r\n");
        fwrite($file,'            }else{'."\r\n");
        fwrite($file,'                validate(validateModel::class)->scene('."'add'".')->check($receiveData);'."\r\n");
        fwrite($file,'            }'."\r\n");
        fwrite($file,'        } catch (ValidateException $e) {'."\r\n");
        fwrite($file,'            jsonFail($e->getError());'."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        if(!empty($receiveData['."'id'])){"."\r\n");
        fwrite($file,'            serviceModel::edit($receiveData);'."\r\n");
        fwrite($file,'        }else{'."\r\n");
        fwrite($file,'            serviceModel::insert($receiveData,$admin['."'username'".'].'.'$admin['."'mobile'".']);'."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        jsonSuccess();'."\r\n");
        fwrite($file,"    } \r\n");
        //修改和编辑结束

        fwrite($file,"\r\n");

        //删除数据开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】删除数据 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function delete(){'."\r\n");
        fwrite($file,'        $fildes = '."'id';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $where = [];'."\r\n");
        fwrite($file,'        $where[] = ['."'id','=',".'$receiveData['."'id']];"."\r\n");
        fwrite($file,'        $where[] = ['."'status','in',[0,1]];"."\r\n");
        fwrite($file,'        $load = serviceModel::getOne($where);'."\r\n");
        fwrite($file,'        if(empty($load)){'."\r\n");
        fwrite($file,'            jsonFail('."'数据不存在');"."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        serviceModel::updateStatus(-1,$load);'."\r\n");
        fwrite($file,'        jsonSuccess();'."\r\n");
        fwrite($file,"    } \r\n");
        //删除数据结束

        fwrite($file,"\r\n");

        //获取单条数据开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】获取数据列表 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function load(){'."\r\n");
        fwrite($file,'        $fildes = '."'id';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $where = [];'."\r\n");
        fwrite($file,'        $where[] = ['."'status','in',[0,1]];"."\r\n");
        fwrite($file,'        $where[] = ['."'id','=',".'$receiveData'."['id']];"."\r\n");
        fwrite($file,'        $data = ['."\r\n");
        fwrite($file,'            '."'model'".' => serviceModel::getOne($where),'."\r\n");
        fwrite($file,'        ];'."\r\n");
        fwrite($file,'        jsonSuccess($data);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取单条数据结束

        fwrite($file,"\r\n");
        fwrite($file,"} \r\n");
        fclose($file);
        return true;
    }

    /*
     * 写入路由
     */
    public static function writeRoute($model,$namespace){
        $modelNamespaceArr = explode('\\',$namespace['admin_controller_namespace']);
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/admin_route/";
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_path = $dir.'/app.php';
        $str = '';
        if(!file_exists($file_path)){
            $createFile = fopen($file_path,'w');
            fwrite($createFile,"<?php"."\r\n");
            fwrite($createFile,"use think\\facade\Route;"."\r\n");
            fclose($createFile);
        }else{
            $fileRead = fopen($file_path,'r');
            $str = fread($fileRead,filesize($file_path));
            fclose($fileRead);
        }
        $fileWrite = fopen($file_path,'a+');
        //后端请求后台controller的名称
        $controller_name = str_replace('_','-',self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $file_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $admin_str = '';
        foreach ($modelNamespaceArr as $item){
            if(!in_array($item,['app','controller'])){
                $admin_str .= $item.'/';
            }
        }
        $admin_str = rtrim($admin_str,'/');
        $listRoute = "Route::rule('$controller_name/list', '$admin_str.$file_name/list');";
        $editRoute = "Route::rule('$controller_name/edit', '$admin_str.$file_name/edit');";
        $deleteRoute = "Route::rule('$controller_name/delete', '$admin_str.$file_name/delete');";
        $loadRoute = "Route::rule('$controller_name/load', '$admin_str.$file_name/load');";
        fwrite($fileWrite,"\r\n");
        if(strpos($str,$listRoute) === false){
            $listRemark = $model['remark'].'获取列表';
            fwrite($fileWrite,$listRoute."//$listRemark"."\r\n");
        }
        if(strpos($str,$editRoute) === false){
            $editRemark = $model['remark'].'编辑';
            fwrite($fileWrite,$editRoute."//$editRemark"."\r\n");
        }
        if(strpos($str,$deleteRoute) === false){
            $deleteRemark = $model['remark'].'删除数据';
            fwrite($fileWrite,$deleteRoute."//$deleteRemark"."\r\n");
        }
        if(strpos($str,$loadRoute) === false){
            $loadRemark = $model['remark'].'获取单条数据';
            fwrite($fileWrite,$loadRoute."//$loadRemark"."\r\n");
        }
        fclose($fileWrite);
        return true;
    }

    /**
     * 【方法注释】生成controller,增加controller方法的同事，还要将路由写入
     * @param $model
     */
    public static function generateApiControllerModel($model,$namespace,$columnList){
        $modelNamespaceArr = explode('\\',$namespace['api_controller_namespace']);
        $module_name = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $table_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/api_controller/".$module_name;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $table_name.'.php';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        $file = fopen($file_path,'w');
        //这里打开和生成的文件，现在开始执行写入文件
        fwrite($file,'<?php'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,'namespace '.$namespace['api_controller_namespace'].';'. "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"use app\BaseController;". "\r\n");
        fwrite($file,"use ".$namespace['validate_namespace']."\\"."$table_name as ValidateModel;". "\r\n");
        fwrite($file,"use ".$namespace['business_namespace']."\\"."$table_name as serviceModel;". "\r\n");
        fwrite($file,"use think\\"."exception\ValidateException;". "\r\n");
        fwrite($file,"\r\n");
        fwrite($file,"class $table_name extends BaseController \r\n");
        fwrite($file,"{ \r\n");
        fwrite($file,"\r\n");
        //获取数据列表查询开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】获取数据列表 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function list(){'."\r\n");
        fwrite($file,'        $fildes = '."'page';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $where = [];'."\r\n");
        fwrite($file,'        $where[] = ['."'status','in',[1]];"."\r\n");
        fwrite($file,'        $limit_start = ($receiveData['."'page']-1) * env('api_page_size');"."\r\n");
        fwrite($file,'        $lists = serviceModel::getList($where,$limit_start,env('."'page_size'));"."\r\n");
        fwrite($file,'        $data = ['."\r\n");
        fwrite($file,'            '."'lists'".' => $lists,'."\r\n");
        fwrite($file,'            '."'is_load'".' => isLoad(count($lists),env('."'api_page_size'))"."\r\n");
        fwrite($file,'        ];'."\r\n");
        fwrite($file,'        jsonSuccess($data);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取数据列表查询结束

        fwrite($file,"\r\n");

        //修改和编辑开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】修改和编辑 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function edit(){'."\r\n");
        $filed_str = "";
        foreach ($columnList as $col){
            if(!in_array($col['column'],['create_time','update_time']) && strpos($col['column_remark'],'[标记字段]') === false){
                $filed_str .= $col['column'].',';
            }
        }
        $filed_str = rtrim($filed_str,',');
        fwrite($file,'        $fildes = '."'$filed_str';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        try{'."\r\n");
        fwrite($file,'            if(!empty($receiveData['."'id'])){"."\r\n");
        fwrite($file,'                validate(validateModel::class)->scene('."'edit'".')->check($receiveData);'."\r\n");
        fwrite($file,'            }else{'."\r\n");
        fwrite($file,'                validate(validateModel::class)->scene('."'add'".')->check($receiveData);'."\r\n");
        fwrite($file,'            }'."\r\n");
        fwrite($file,'        } catch (ValidateException $e) {'."\r\n");
        fwrite($file,'            jsonFail($e->getError());'."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        if(!empty($receiveData['."'id'])){"."\r\n");
        fwrite($file,'            serviceModel::edit($receiveData,true);'."\r\n");
        fwrite($file,'        }else{'."\r\n");
        fwrite($file,'            serviceModel::insert($receiveData,"",true);'."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        jsonSuccess();'."\r\n");
        fwrite($file,"    } \r\n");
        //修改和编辑结束

        fwrite($file,"\r\n");

        //删除数据开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】删除数据 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function delete(){'."\r\n");
        fwrite($file,'        $fildes = '."'id';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $where = [];'."\r\n");
        fwrite($file,'        $where[] = ['."'id','=',".'$receiveData['."'id']];"."\r\n");
        fwrite($file,'        $where[] = ['."'status','in',[1]];"."\r\n");
        fwrite($file,'        $load = serviceModel::getOne($where);'."\r\n");
        fwrite($file,'        if(empty($load)){'."\r\n");
        fwrite($file,'            jsonFail('."'数据不存在');"."\r\n");
        fwrite($file,'        }'."\r\n");
        fwrite($file,'        serviceModel::updateStatus(-1,$load);'."\r\n");
        fwrite($file,'        jsonSuccess();'."\r\n");
        fwrite($file,"    } \r\n");
        //删除数据结束

        fwrite($file,"\r\n");

        //获取单条数据开始
        fwrite($file,"    /* \r\n");
        fwrite($file,"     *【方法注释】获取数据列表 \r\n");
        fwrite($file,'     */'. "\r\n");
        fwrite($file,'    public function load(){'."\r\n");
        fwrite($file,'        $fildes = '."'id';"."\r\n");
        fwrite($file,'        $receiveData = getData($this->request->post(),$fildes);'."\r\n");
        fwrite($file,'        $where = [];'."\r\n");
        fwrite($file,'        $where[] = ['."'status','in',[1]];"."\r\n");
        fwrite($file,'        $where[] = ['."'id','=',".'$receiveData'."['id']];"."\r\n");
        fwrite($file,'        $data = ['."\r\n");
        fwrite($file,'            '."'model'".' => serviceModel::getOne($where),'."\r\n");
        fwrite($file,'        ];'."\r\n");
        fwrite($file,'        jsonSuccess($data);'."\r\n");
        fwrite($file,"    } \r\n");
        //获取单条数据结束

        fwrite($file,"\r\n");
        fwrite($file,"} \r\n");
        fclose($file);
        return true;
    }

    /*
     * 生成SQL菜单语句
     */
    public static function generateSqlMenu(){
        $namespaceList = CommonTableNamespace::getListAll(['status'=>1],'*');
        if(count($namespaceList) > 0){
            $result = [];
            $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend";
            if(!file_exists($dir)){
                mkdir($dir,0777,true);
            }
            $file_name = $dir.'/menuFunc.php';
            foreach ($namespaceList as $namespace){
                $result[] = [
                    'title' => $namespace['remark'],
                    'path' => '',
                    'parent_id' => 0
                ];
                $where = [];
                $where[] = ['status','in',[1]];
                $where[] = ['table_namespace_id','=',$namespace['id']];
                $lists = self::getListAll($where);
                if(count($lists) > 0){
                    foreach ($lists as $item){
                        $controller_name = str_replace('_','-',self::cutTablePrefix($item['table_name'],$namespace['table_file_prefix']));
                        $result[] = [
                            'title' => $item['remark'],
                            'path' => '/'.$namespace['front_api_name'].'/'.$controller_name.'/',
                            'parent_id' => $namespace['remark']
                        ];
                    }
                }
            }
            $file = fopen($file_name,'w');
            fwrite($file,"    public function test(){ \r\n");
            fwrite($file,'        $str = '."'".json_encode($result)."'". "\r\n");
            fwrite($file,'        $jsonStr = json_decode($str,true);'. "\r\n");
            fwrite($file,'        foreach ($jsonStr as $item){'. "\r\n");
            fwrite($file,'            if(empty($item['."'parent_id'])){". "\r\n");
            fwrite($file,'                $data = ['. "\r\n");
            fwrite($file,"                    'title' =>". '$item['."'title'],". "\r\n");
            fwrite($file,"                    'path' => '',". "\r\n");
            fwrite($file,"                    'parent_id' => 0,". "\r\n");
            fwrite($file,"                    'status' => 1,". "\r\n");
            fwrite($file,"                    'create_time' => date('Y-m-d H:i:s'),". "\r\n");
            fwrite($file,"                    'update_time' => date('Y-m-d H:i:s'),". "\r\n");
            fwrite($file,'                ];'. "\r\n");
            fwrite($file,'                \app\common\model\auth\CommonAdminMenu::insert($data);'. "\r\n");
            fwrite($file,'            }else{'. "\r\n");
            fwrite($file,'                $load = \app\common\model\auth\CommonAdminMenu::where(['."'title'=>".'$item['."'parent_id']])->find();". "\r\n");
            fwrite($file,'                if(!empty($load)){'. "\r\n");
            fwrite($file,'                    $data = ['. "\r\n");
            fwrite($file,"                        'title' =>". '$item['."'title'],". "\r\n");
            fwrite($file,"                        'path' =>". '$item['."'path'],". "\r\n");
            fwrite($file,"                        'parent_id' =>". '$load['."'id'],". "\r\n");
            fwrite($file,"                        'status' => 1,". "\r\n");
            fwrite($file,"                        'create_time' => date('Y-m-d H:i:s'),". "\r\n");
            fwrite($file,"                        'update_time' => date('Y-m-d H:i:s'),". "\r\n");
            fwrite($file,'                    ];'. "\r\n");
            fwrite($file,'                    \app\common\model\auth\CommonAdminMenu::insert($data);'. "\r\n");
            fwrite($file,'                }'. "\r\n");
            fwrite($file,'            }'. "\r\n");
            fwrite($file,"        } \r\n");
            fwrite($file,"    } \r\n");
            fclose($file);
        }
        return true;
    }

    /*
     * 生成前端模块路由信息
     */
    public static function generateFrontRoute(){
        $namespaceList = CommonTableNamespace::getListAll(['status'=>1],'*');
        if(count($namespaceList) > 0){
            $dir = $_SERVER['DOCUMENT_ROOT']."/generate/front";
            if(!file_exists($dir)){
                mkdir($dir,0777,true);
            }
            $file_name = 'index.js';
            $file_path = $dir.'/'.$file_name;
            if(!file_exists($file_path)){
                $createFile = fopen($file_path,'w');
                fclose($createFile);
            }
            foreach ($namespaceList as $namespace){
                $where = [];
                $where[] = ['status','in',[1]];
                $where[] = ['table_namespace_id','=',$namespace['id']];
                $lists = self::getListAll($where);
                if(count($lists) > 0){
                    $file = fopen($file_path,'a+');
                    fwrite($file,"    { \r\n");
                    fwrite($file,"        path: '/".$namespace['front_api_name']."', \r\n");
                    fwrite($file,"        component: Home, \r\n");
                    fwrite($file,"        meta: { \r\n");
                    fwrite($file,"            title: '".$namespace['remark']."', \r\n");
                    fwrite($file,"        }, \r\n");
                    fwrite($file,"        children: [ \r\n");
                    foreach ($lists as $item){
                        $controller_name = str_replace('_','-',self::cutTablePrefix($item['table_name'],$namespace['table_file_prefix']));
                        $table_name = self::getTableFileName(self::cutTablePrefix($item['table_name'],$namespace['table_file_prefix']));
                        fwrite($file,"            { \r\n");
                        fwrite($file,"                path: '$controller_name', \r\n");
                        fwrite($file,"                component: () => import('@/pages/".$namespace['front_api_name']."/".$table_name."'), \r\n");
                        fwrite($file,"                meta: { \r\n");
                        fwrite($file,"                        title: '".$item['remark']."' \r\n");
                        fwrite($file,"                } \r\n");
                        fwrite($file,"            }, \r\n");
                    }
                    fwrite($file,"        ] \r\n");
                    fwrite($file,"    }, \r\n");
                    fclose($file);
                }
            }
        }
        return true;
    }

    /*
     * 写入路由
     */
    public static function writeApiRoute($model,$namespace){
        $modelNamespaceArr = explode('\\',$namespace['api_controller_namespace']);
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/backend/api_route/";
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_path = $dir.'/app.php';
        $str = '';
        if(!file_exists($file_path)){
            $createFile = fopen($file_path,'w');
            fwrite($createFile,"<?php"."\r\n");
            fwrite($createFile,"use think\\facade\Route;"."\r\n");
            fclose($createFile);
        }else{
            $fileRead = fopen($file_path,'r');
            $str = fread($fileRead,filesize($file_path));
            fclose($fileRead);
        }
        $fileWrite = fopen($file_path,'a+');
        //后端请求后台controller的名称
        $controller_name = str_replace('_','-',self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $file_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $admin_str = '';
        foreach ($modelNamespaceArr as $item){
            if(!in_array($item,['app','controller'])){
                $admin_str .= $item.'/';
            }
        }
        $admin_str = rtrim($admin_str,'/');
        $listRoute = "Route::rule('$controller_name/list', '$admin_str.$file_name/list');";
        $editRoute = "Route::rule('$controller_name/edit', '$admin_str.$file_name/edit');";
        $deleteRoute = "Route::rule('$controller_name/delete', '$admin_str.$file_name/delete');";
        $loadRoute = "Route::rule('$controller_name/load', '$admin_str.$file_name/load');";
        fwrite($fileWrite,"\r\n");
        if(strpos($str,$listRoute) === false){
            $listRemark = $model['remark'].'获取列表';
            fwrite($fileWrite,$listRoute."//$listRemark"."\r\n");
        }
        if(strpos($str,$editRoute) === false){
            $editRemark = $model['remark'].'编辑';
            fwrite($fileWrite,$editRoute."//$editRemark"."\r\n");
        }
        if(strpos($str,$deleteRoute) === false){
            $deleteRemark = $model['remark'].'删除数据';
            fwrite($fileWrite,$deleteRoute."//$deleteRemark"."\r\n");
        }
        if(strpos($str,$loadRoute) === false){
            $loadRemark = $model['remark'].'获取单条数据';
            fwrite($fileWrite,$loadRoute."//$loadRemark"."\r\n");
        }
        fclose($fileWrite);
        return true;
    }

    /*
     * 生成前端API文件
     */
    public static function generateFrontApi($model,$namespace){
        $modelNamespaceArr = explode('\\',$namespace['admin_controller_namespace']);
        $module_name_x = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $module_name = camelize(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/front/api/".$module_name_x;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $module_name.'Api.js';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        //前端请求后台controller的名称
        $controller_name = str_replace('_','-',self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $file = fopen($file_path,'w');
        fwrite($file,"import axios from '@/utils/request';"."\r\n");
        fwrite($file,"\r\n");

        //list列表的url定义开始
        $getListName = 'get'.self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix'])).'List';
        fwrite($file,"export function $getListName(params) {"."\r\n");
        fwrite($file,"    return axios.post({"."\r\n");
        fwrite($file,"        url: '/$controller_name/list',"."\r\n");
        fwrite($file,"        data: params"."\r\n");
        fwrite($file,"    }).then((res) => {"."\r\n");
        fwrite($file,"        return res"."\r\n");
        fwrite($file,"    })"."\r\n");
        fwrite($file,"}"."\r\n");
        //list列表的url定义结束

        fwrite($file,"\r\n");

        //edit的url定义开始
        $editName = 'edit'.self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        fwrite($file,"export function $editName(params) {"."\r\n");
        fwrite($file,"    return axios.post({"."\r\n");
        fwrite($file,"        url: '/$controller_name/edit',"."\r\n");
        fwrite($file,"        data: params"."\r\n");
        fwrite($file,"    }).then((res) => {"."\r\n");
        fwrite($file,"        return res"."\r\n");
        fwrite($file,"    })"."\r\n");
        fwrite($file,"}"."\r\n");
        //edit的url定义开始

        fwrite($file,"\r\n");

        //delete的url定义开始
        $deleteName = 'delete'.self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        fwrite($file,"export function $deleteName(params) {"."\r\n");
        fwrite($file,"    return axios.post({"."\r\n");
        fwrite($file,"        url: '/$controller_name/delete',"."\r\n");
        fwrite($file,"        data: params"."\r\n");
        fwrite($file,"    }).then((res) => {"."\r\n");
        fwrite($file,"        return res"."\r\n");
        fwrite($file,"    })"."\r\n");
        fwrite($file,"}"."\r\n");
        //delete的url定义开始

        fwrite($file,"\r\n");

        //updateStatus的url定义开始
        $getOneName = 'load'.self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        fwrite($file,"export function $getOneName(params) {"."\r\n");
        fwrite($file,"    return axios.post({"."\r\n");
        fwrite($file,"        url: '/$controller_name/load',"."\r\n");
        fwrite($file,"        data: params"."\r\n");
        fwrite($file,"    }).then((res) => {"."\r\n");
        fwrite($file,"        return res"."\r\n");
        fwrite($file,"    })"."\r\n");
        fwrite($file,"}"."\r\n");
        //updateStatus的url定义开始

        fwrite($file,"\r\n");

        fclose($file);
        return true;
    }

    /*
     * 生成页面
     */
    public static function generateFrontPage($model,$namespace,$columnList){
        $modelNamespaceArr = explode('\\',$namespace['admin_controller_namespace']);
        $module_name_x = $modelNamespaceArr[count($modelNamespaceArr)-1];
        $module_name = camelize(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $table_name = self::getTableFileName(self::cutTablePrefix($model['table_name'],$namespace['table_file_prefix']));
        $dir = $_SERVER['DOCUMENT_ROOT']."/generate/front/page/".$module_name_x;
        if(!file_exists($dir)){
            mkdir($dir,0777,true);
        }
        $file_name = $table_name.'.vue';
        $apiName = $module_name.'Api';
        $file_path = $dir.'/'.$file_name;
        if(file_exists($file_path)){
            unlink($file_path);
        }
        //前端请求后台controller的名称
        $getListName = 'get'.$table_name.'List';
        $editName = 'edit'.$table_name;
        $deleteName = 'delete'.$table_name;
        $file = fopen($file_path,'w');
        fwrite($file,"<template>"."\r\n");
        fwrite($file,"    <div class='page-content'>"."\r\n");
        fwrite($file,'        <table-bar :showTop="true" :columns="columns" @changeColumn="changeColumn">'."\r\n");
        fwrite($file,'            <div slot="top">'."\r\n");
        fwrite($file,'                <el-form label-width="90px">'."\r\n");
        fwrite($file,'                    <el-row :gutter="20">'."\r\n");
        fwrite($file,'                        <el-col :xs="24" :sm="12" :lg="6">'."\r\n");
        fwrite($file,'                            <el-form-item label="状态：">'."\r\n");
        fwrite($file,'                                <el-select v-model="status" style="width:100%" placeholder="请选择状态">'."\r\n");
        fwrite($file,'                                    <el-option label="禁用" value="1"></el-option>'."\r\n");
        fwrite($file,'                                    <el-option label="正常" value="2"></el-option>'."\r\n");
        fwrite($file,'                                </el-select>'."\r\n");
        fwrite($file,'                            </el-form-item>'."\r\n");
        fwrite($file,'                        </el-col>'."\r\n");
        fwrite($file,'                        <el-row :xs="24" :sm="12" :lg="6" style="float: right; margin-right: 10px;">'."\r\n");
        fwrite($file,'                            <el-button type="primary" @click="search">查询</el-button>'."\r\n");
        fwrite($file,'                            <el-button @click="reset">重置</el-button>'."\r\n");
        fwrite($file,'                        </el-row>'."\r\n");
        fwrite($file,'                    </el-row>'."\r\n");
        fwrite($file,'                </el-form>'."\r\n");
        fwrite($file,'            </div>'."\r\n");
        fwrite($file,'            <div slot="bottom">'."\r\n");
        fwrite($file,'                <el-button type="primary" plain @click="showDialog">新增</el-button>'."\r\n");
        fwrite($file,'            </div>'."\r\n");
        fwrite($file,'        </table-bar>'."\r\n");

        //表格开始
        fwrite($file,'        <el-table :data="lists" :showPage="false" border ref="table">'."\r\n");
        fwrite($file,'            <el-table-column label="ID" width="80" prop="id" v-if="columns[0].show"/>'."\r\n");
        $tableNumber = 1;
        foreach ($columnList as $col){
            if(!in_array($col['column'],['id','create_time','status','remark','update_time'])){
                if(strpos($col['column_remark'],'[富文本]') !== false){
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    $column_remark = trim(str_replace('[单图上传]','',$col['column_remark']));
                    fwrite($file,'            <el-table-column label="'.$column_remark.'" width="100" prop="'.$col['column'].'" v-if="columns['.$tableNumber.'].show">'."\r\n");
                    fwrite($file,'                <template slot-scope="scope">'."\r\n");
                    fwrite($file,'                    <img :src="scope.row.'.$col['column'].'" style="width: 60px;height: 50px;" @click="clickBig(scope.row.'.$col['column'].')"/>'."\r\n");
                    fwrite($file,'                </template>'."\r\n");
                    fwrite($file,'            </el-table-column>'."\r\n");
                    $tableNumber ++;
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false) {
                    $column_remark = trim(str_replace('[多图上传]', '', $col['column_remark']));
                    fwrite($file, '            <el-table-column label="' . $column_remark . '" width="100" prop="' . $col['column'] . '" v-if="columns[' . $tableNumber . '].show">' . "\r\n");
                    fwrite($file, '                <template slot-scope="scope">' . "\r\n");
                    fwrite($file, '                    <template v-for="item in scope.row.' . $col['column'] . '">' . "\r\n");
                    fwrite($file, '                        <img :src="item" style="width: 60px;height: 50px;" @click="clickBig(item)"/>' . "\r\n");
                    fwrite($file, '                    </template>' . "\r\n");
                    fwrite($file, '                </template>' . "\r\n");
                    fwrite($file, '            </el-table-column>' . "\r\n");
                    $tableNumber++;
                }elseif(strpos($col['column_remark'],'[radio]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = trim(str_replace($pattern_str,'',str_replace('[radio]','',$col['column_remark'])));
                    fwrite($file,'            <el-table-column label="'.$column_remark.'" width="120" prop="'.$col['column'].'_name" v-if="columns['.$tableNumber.'].show"/>'."\r\n");
                    $tableNumber++;
                }elseif(strpos($col['column_remark'],'[select]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = trim(str_replace($pattern_str,'',str_replace('[select]','',$col['column_remark'])));
                    fwrite($file,'            <el-table-column label="'.$column_remark.'" width="120" prop="'.$col['column'].'_name" v-if="columns['.$tableNumber.'].show"/>'."\r\n");
                    $tableNumber++;
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false) {
                        fwrite($file,'            <el-table-column label="'.$col['column_remark'].'" width="120" prop="'.$col['column'].'" v-if="columns['.$tableNumber.'].show"/>'."\r\n");
                        $tableNumber ++;
                    }
                }
            }
        }
        fwrite($file,'            <el-table-column label="状态" width="80" prop="status" v-if="columns['.($tableNumber).'].show">'."\r\n");
        fwrite($file,'                <template slot-scope="scope">'."\r\n");
        fwrite($file,'                    <template v-if="scope.row.status == 1">'."\r\n");
        fwrite($file,'                        <el-tag size="mini" type="success">正常</el-tag>'."\r\n");
        fwrite($file,'                    </template>'."\r\n");
        fwrite($file,'                    <template v-else>'."\r\n");
        fwrite($file,'                        <el-tag size="mini" type="danger">禁用</el-tag>'."\r\n");
        fwrite($file,'                    </template>'."\r\n");
        fwrite($file,'                </template>'."\r\n");
        fwrite($file,'            </el-table-column>'."\r\n");
        fwrite($file,'            <el-table-column label="备注" width="100" prop="remark" v-if="columns['.($tableNumber+1).'].show"/>'."\r\n");
        fwrite($file,'            <el-table-column label="创建时间" width="140" prop="create_time" v-if="columns['.($tableNumber+2).'].show"/>'."\r\n");
        fwrite($file,'            <el-table-column label="更新时间" width="140" prop="update_time" v-if="columns['.($tableNumber+3).'].show"/>'."\r\n");
        fwrite($file,'            <el-table-column fixed="right" label="操作" width="160">'."\r\n");
        fwrite($file,'                <template slot-scope="scope">'."\r\n");
        fwrite($file,'                    <el-button type="primary" @click="updateModel(scope.row)">修改</el-button>'."\r\n");
        fwrite($file,'                    <el-button type="danger" @click="deleteModel(scope.row)">删除</el-button>'."\r\n");
        fwrite($file,'                </template>'."\r\n");
        fwrite($file,'            </el-table-column>'."\r\n");
        fwrite($file,'        </el-table>'."\r\n");
        //表格结束

        //分页开始
        fwrite($file,'        <template v-if="allpage>10">'."\r\n");
        fwrite($file,'            <el-pagination @current-change="handleCurrentChange"'."\r\n");
        fwrite($file,'                            :current-page="1"'."\r\n");
        fwrite($file,'                            :page-size="10"'."\r\n");
        fwrite($file,'                            layout="total, prev, pager, next, jumper"'."\r\n");
        fwrite($file,'                            :total="allpage">'."\r\n");
        fwrite($file,'            </el-pagination>'."\r\n");
        fwrite($file,'        </template>'."\r\n");
        //分页结束

        //定义表单是否包含富文本
        $isEditor = false;
        //定义表单是否表单单图上传
        $isOnlyImageUpload = false;
        $onlyImageUoloadList = [];
        $onlyImageUploadNumber = 0;
        //定义表单是否包含多图上传
        $isMoreImageUpload = false;
        $moreImageUoloadList = [];
        $moreImageUploadNumber = 0;

        //编辑数据开始
        fwrite($file,'        <el-dialog title="编辑" width="40%" :visible.sync="dvEdit" :before-close="handleClose">'."\r\n");
        fwrite($file,'            <el-form ref="model" :model="model" label-width="80px">'."\r\n");
        foreach ($columnList as $col){
            if(!in_array($col['column'],['id','create_time','status','remark','update_time'])){
                if(strpos($col['column_remark'],'[富文本]') !== false){
                    $column_remark = trim(str_replace('[富文本]','',$col['column_remark']));
                    fwrite($file,'                <el-form-item label="'.$column_remark.'">'."\r\n");
                    fwrite($file,'                    <quill-editor class="editor" ref="quillEditor" v-model="model.'.$col['column'].'" :options="editorOption"/>'."\r\n");
                    fwrite($file,'                </el-form-item>'."\r\n");
                    $isEditor = true;
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    $column_remark = trim(str_replace('[单图上传]','',$col['column_remark']));
                    if($onlyImageUploadNumber == 0){
                        $onRemove = "handleRemove";
                        $beforeRemove = "beforeRemove";
                        $onSuccess = "handleAvatarSuccess";
                    }else{
                        $onRemove = "handleRemove".$onlyImageUploadNumber;
                        $beforeRemove = "beforeRemove".$onlyImageUploadNumber;
                        $onSuccess = "handleAvatarSuccess".$onlyImageUploadNumber;
                    }
                    fwrite($file,'                <el-form-item label="'.$column_remark.'">'."\r\n");
                    fwrite($file,'                    <span style="color:red">注意：只能上传jpg/png文件，建议尺寸400*400</span>'."\r\n");
                    fwrite($file,'                    <el-upload'."\r\n");
                    fwrite($file,'                      class="avatar-uploader"'."\r\n");
                    fwrite($file,'                      :action="uploadImage"'."\r\n");
                    fwrite($file,'                      accept=".jpg,.png"'."\r\n");
                    fwrite($file,'                      name="file"'."\r\n");
                    fwrite($file,'                      :show-file-list="false"'."\r\n");
                    fwrite($file,'                      :on-remove="'.$onRemove.'"'."\r\n");
                    fwrite($file,'                      :before-remove="'.$beforeRemove.'"'."\r\n");
                    fwrite($file,'                      :on-success="'.$onSuccess.'">'."\r\n");
                    fwrite($file,'                        <img v-if="'.$col['column'].'" :src="'.$col['column'].'" style="width:200px;height: 200px;">'."\r\n");
                    fwrite($file,'                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>'."\r\n");
                    fwrite($file,'                    </el-upload>'."\r\n");
                    fwrite($file,'                </el-form-item>'."\r\n");
                    $isOnlyImageUpload = true;
                    $onlyImageUoloadList[] = $col['column'];
                    $onlyImageUploadNumber ++;
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false) {
                    $column_remark = trim(str_replace('[多图上传]', '', $col['column_remark']));
                    if ($moreImageUploadNumber == 0) {
                        $httpRequest = "uploadFile";
                        $onRemove = "moreHandleRemove";
                    } else {
                        $httpRequest = "uploadFile" . $moreImageUploadNumber;
                        $onRemove = "moreHandleRemove" . $moreImageUploadNumber;
                    }
                    fwrite($file, '                <el-form-item label="' . $column_remark . '">' . "\r\n");
                    fwrite($file, '                    <span style="color:red">注意：只能上传jpg/png文件，建议尺寸400*400</span>' . "\r\n");
                    fwrite($file, '                    <el-upload' . "\r\n");
                    fwrite($file, '                      enctype="multipart/form-data"' . "\r\n");
                    fwrite($file, '                      class="upload-demo"' . "\r\n");
                    fwrite($file, '                      :action="uploadImage"' . "\r\n");
                    fwrite($file, '                      accept=".jpg,.png"' . "\r\n");
                    fwrite($file, '                      name="file"' . "\r\n");
                    fwrite($file, '                      :limit="10"' . "\r\n");
                    fwrite($file, '                      multiple' . "\r\n");
                    fwrite($file, '                      :file-list="' . $col['column'] . '"' . "\r\n");
                    fwrite($file, '                      :on-remove="' . $onRemove . '"' . "\r\n");
                    fwrite($file, '                      :http-request="' . $httpRequest . '"' . "\r\n");
                    fwrite($file, '                      list-type="picture">' . "\r\n");
                    fwrite($file, '                        <el-button size="small" type="primary">点击上传</el-button>' . "\r\n");
                    fwrite($file, '                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2M,宽度750</div>' . "\r\n");
                    fwrite($file, '                    </el-upload>' . "\r\n");
                    fwrite($file, '                </el-form-item>' . "\r\n");
                    $isMoreImageUpload = true;
                    $moreImageUoloadList[] = $col['column'];
                    $moreImageUploadNumber++;
                }elseif(strpos($col['column_remark'],'[radio]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = trim(str_replace($pattern_str,'',str_replace('[radio]','',$col['column_remark'])));
                    fwrite($file,'                <el-form-item label="'.$column_remark.'">'."\r\n");
                    fwrite($file,'                    <el-radio-group v-model="model.'.$col['column'].'" style="width:100%" placeholder="请选择'.$column_remark.'">'."\r\n");
                    $pattern_str = trim(str_replace('】','',str_replace('【','',$pattern_str)));
                    if(!empty($pattern_str)){
                        $pattern_arr = explode('|',$pattern_str);
                        if(count($pattern_arr) > 0){
                            fwrite($file,'        $arr = ['."\r\n");
                            foreach ($pattern_arr as $pattern){
                                if(!empty($pattern)){
                                    $item_arr = explode('-',$pattern);
                                    if(count($item_arr) > 1) {
                                        fwrite($file,'                    <el-radio label="'.$item_arr[0].'">'.$item_arr[1].'</el-radio>'."\r\n");
                                    }else {
                                        fwrite($file,'                    <el-radio label="'.$item_arr[0].'"></el-radio>'."\r\n");
                                    }
                                }
                            }
                        }else{
                        }
                    }
                    fwrite($file,'                    </el-radio-group>'."\r\n");
                    fwrite($file,'                </el-form-item>'."\r\n");
                }elseif(strpos($col['column_remark'],'[select]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = trim(str_replace($pattern_str,'',str_replace('[select]','',$col['column_remark'])));
                    fwrite($file,'                <el-form-item label="'.$column_remark.'">'."\r\n");
                    fwrite($file,'                    <el-select v-model="model.'.$col['column'].'" style="width:100%" placeholder="请选择'.$column_remark.'">'."\r\n");
                    $pattern_str = trim(str_replace('】','',str_replace('【','',$pattern_str)));
                    if(!empty($pattern_str)){
                        $pattern_arr = explode('|',$pattern_str);
                        if(count($pattern_arr) > 0){
                            foreach ($pattern_arr as $pattern){
                                if(!empty($pattern)){
                                    $item_arr = explode('-',$pattern);
                                    if(count($item_arr) > 1) {
                                        fwrite($file,'                    <el-option label="'.$item_arr[1].'" value="'.$item_arr[0].'"></el-option>'."\r\n");
                                    }else {
                                        fwrite($file,'                    <el-option label="" value="'.$item_arr[0].'"></el-option>'."\r\n");
                                    }
                                }
                            }
                        }else{
                        }
                    }
                    fwrite($file,'                    </el-select>'."\r\n");
                    fwrite($file,'                </el-form-item>'."\r\n");
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false) {
                        switch ($col['data_type']){
                            case 'datetime':
                                fwrite($file,'                <el-form-item label="'.$col['column_remark'].'">'."\r\n");
                                fwrite($file,'                    <el-date-picker type="datetime" placeholder="请选择时间" format="yyyy-MM-dd HH:mm:ss"
                                                value-format="yyyy-MM-dd HH:mm:ss" suffix-icon="el-icon-date" :editable="false"
                                                v-model="model.'.$col['column'].'"></el-date-picker>'."\r\n");
                                fwrite($file,'                </el-form-item>'."\r\n");
                                break;
                            case 'date':
                                fwrite($file,'                <el-form-item label="'.$col['column_remark'].'">'."\r\n");
                                fwrite($file,'                    <el-date-picker type="date" placeholder="请选择日期" format="yyyy-MM-dd"
                                                value-format="yyyy-MM-dd" suffix-icon="el-icon-date" :editable="false"
                                                v-model="model.'.$col['column'].'"></el-date-picker>'."\r\n");
                                fwrite($file,'                </el-form-item>'."\r\n");
                                break;
                            case 'time':
                                fwrite($file,'                <el-form-item label="'.$col['column_remark'].'">'."\r\n");
                                fwrite($file,'                    <el-time-picker type="datetime" placeholder="请选择时间点" format="HH:mm:ss"
                                                value-format="HH:mm:ss" v-model="model.'.$col['column'].'"></el-time-picker>'."\r\n");
                                fwrite($file,'                </el-form-item>'."\r\n");
                                break;
                            case 'int':
                            case 'decimal':
                            case 'double':
                            case 'float':
                            fwrite($file,'                <el-form-item label="'.$col['column_remark'].'">'."\r\n");
                            fwrite($file,'                    <el-input v-model="model.'.$col['column'].'" type="number" placeholder="请输入'.$col['column_remark'].'" style="width:100%"></el-input>'."\r\n");
                            fwrite($file,'                </el-form-item>'."\r\n");
                                break;
                            default:
                                if($col['column'] != 'admin_name'){
                                    fwrite($file,'                <el-form-item label="'.$col['column_remark'].'">'."\r\n");
                                    fwrite($file,'                    <el-input v-model="model.'.$col['column'].'" type="text" placeholder="请输入'.$col['column_remark'].'" style="width:100%"></el-input>'."\r\n");
                                    fwrite($file,'                </el-form-item>'."\r\n");
                                }
                                break;
                        }
                    }
                }
            }
        }
        fwrite($file,'                <el-form-item label="备注">'."\r\n");
        fwrite($file,'                    <el-input v-model="model.remark" type="text" placeholder="请输入备注" style="width:100%"></el-input>'."\r\n");
        fwrite($file,'                </el-form-item>'."\r\n");
        fwrite($file,'                <el-form-item label="状态">'."\r\n");
        fwrite($file,'                    <el-select v-model="model.status" style="width:100%" placeholder="请选择状态">'."\r\n");
        fwrite($file,'                    <el-option label="禁用" value="0"></el-option>'."\r\n");
        fwrite($file,'                    <el-option label="正常" value="1"></el-option>'."\r\n");
        fwrite($file,'                    </el-select>'."\r\n");
        fwrite($file,'                </el-form-item>'."\r\n");
        fwrite($file,'            </el-form>'."\r\n");
        fwrite($file,'            <span slot="footer" class="dialog-footer">'."\r\n");
        fwrite($file,'                <el-button @click="dvEdit = false">取 消</el-button>'."\r\n");
        fwrite($file,'                <el-button type="primary" @click="onSubmit">确 定</el-button>'."\r\n");
        fwrite($file,'            </span>'."\r\n");
        fwrite($file,'        </el-dialog>'."\r\n");
        //编辑数据结束

        if($isOnlyImageUpload || $isMoreImageUpload){
            fwrite($file,'        <el-dialog :visible.sync="dialogBigVisible" width="60%">'."\r\n");
            fwrite($file,'            <img :src="bigSrc" style="width:100%;display: block">'."\r\n");
            fwrite($file,'        </el-dialog>'."\r\n");
        }
        fwrite($file,"    </div>"."\r\n");
        fwrite($file,"</template>"."\r\n");

        fwrite($file,"\r\n");
        fwrite($file,"\r\n");

        //引入的处理开始
        fwrite($file,"<script>"."\r\n");
        fwrite($file,"    import {".$getListName.",".$editName.",".$deleteName."} from '@/api/$module_name_x/$apiName';"."\r\n");
        if($isOnlyImageUpload || $isMoreImageUpload){
            fwrite($file,'    import {uploadImageUrl} from "@/utils/config.js";'."\r\n");
        }
        if($isMoreImageUpload){
            fwrite($file,'    import axios from "axios";'."\r\n");
        }
        if($isEditor){
            fwrite($file,'    import quillConfig from "@/utils/quill-config.js";'."\r\n");
            fwrite($file,"    import {quillEditor} from 'vue-quill-editor';"."\r\n");
            fwrite($file,"    import 'quill/dist/quill.core.css';"."\r\n");
            fwrite($file,"    import 'quill/dist/quill.snow.css';"."\r\n");
            fwrite($file,"    import 'quill/dist/quill.bubble.css';"."\r\n");
            fwrite($file,"    import '@/config/quill/quill.scss';"."\r\n");
        }
        //引入的处理结束

        fwrite($file,"\r\n");

        fwrite($file,"export default {"."\r\n");
        if($isEditor){
            fwrite($file,"    components: {"."\r\n");
            fwrite($file,"        quillEditor"."\r\n");
            fwrite($file,"    },"."\r\n");
        }
        fwrite($file,"    data() {"."\r\n");
        fwrite($file,"        return {"."\r\n");
        fwrite($file,"            lists: [],"."\r\n");
        fwrite($file,"            allpage: 1,//总页数"."\r\n");
        fwrite($file,"            showItem: 5,//分页显示得中间按钮个数"."\r\n");
        fwrite($file,"            current: 1,//当前页"."\r\n");
        fwrite($file,"            status: '',"."\r\n");
        fwrite($file,"            model: {},"."\r\n");
        fwrite($file,"            dvEdit: false,"."\r\n");
        if($isEditor){
            fwrite($file,"            editorOption: quillConfig,"."\r\n");
        }
        if($isMoreImageUpload || $isOnlyImageUpload){
            fwrite($file,"            dialogBigVisible: false,"."\r\n");
            fwrite($file,"            bigSrc: '',"."\r\n");
            fwrite($file,"            uploadImage: uploadImageUrl,"."\r\n");
        }
        if(count($moreImageUoloadList) > 0){
            foreach ($moreImageUoloadList as $moreImageName){
                fwrite($file,"            $moreImageName: [],"."\r\n");
            }
        }
        if(count($onlyImageUoloadList) > 0){
            foreach ($onlyImageUoloadList as $onlyImageName){
                fwrite($file,"            $onlyImageName: '',"."\r\n");
            }
        }
        fwrite($file,"            columns: ["."\r\n");
        fwrite($file,'                {name: "ID", show: true},'."\r\n");
        foreach ($columnList as $col){
            if(!in_array($col['column'],['id','create_time','status','remark','update_time'])){
                if(strpos($col['column_remark'],'[富文本]') !== false){
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    $column_remark = trim(str_replace('[单图上传]','',$col['column_remark']));
                    fwrite($file,'                {name: "'.$column_remark.'", show: true},'."\r\n");
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false){
                    $column_remark = trim(str_replace('[多图上传]','',$col['column_remark']));
                    fwrite($file,'                {name: "'.$column_remark.'", show: true},'."\r\n");
                }elseif(strpos($col['column_remark'],'[radio]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = trim(str_replace($pattern_str,'',str_replace('[radio]','',$col['column_remark'])));
                    fwrite($file,'                {name: "'.$column_remark.'", show: true},'."\r\n");
                }elseif(strpos($col['column_remark'],'[select]') !== false){
                    $pattern_str = self::getPatternStr($col['column_remark']);
                    $column_remark = trim(str_replace($pattern_str,'',str_replace('[select]','',$col['column_remark'])));
                    fwrite($file,'                {name: "'.$column_remark.'", show: true},'."\r\n");
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false) {
                        fwrite($file,'                {name: "'.$col['column_remark'].'", show: true},'."\r\n");
                    }
                }
            }
        }
        fwrite($file,'                {name: "状态", show: true},'."\r\n");
        fwrite($file,'                {name: "备注", show: true},'."\r\n");
        fwrite($file,'                {name: "创建时间", show: true},'."\r\n");
        fwrite($file,'                {name: "更新时间", show: true},'."\r\n");
        fwrite($file,"            ],"."\r\n");
        fwrite($file,"        };"."\r\n");
        fwrite($file,"    },"."\r\n");

        //加载数据开始
        fwrite($file,"    created: function () {"."\r\n");
        fwrite($file,"        this.handleCurrentChange(1);"."\r\n");
        fwrite($file,"    },"."\r\n");
        //加载数据结束

        //方法定义开始
        fwrite($file,"    methods: {"."\r\n");

        //处理点图图片方法的方法
        if($isOnlyImageUpload || $isMoreImageUpload){
            fwrite($file,"        clickBig: function (img) {"."\r\n");
            fwrite($file,"            this.bigSrc = img;"."\r\n");
            fwrite($file,"            this.dialogBigVisible = true;"."\r\n");
            fwrite($file,"        },"."\r\n");
        }

        //分页的方法
        fwrite($file,"        handleCurrentChange: function (index) {"."\r\n");
        fwrite($file,"            this.current = index;"."\r\n");
        fwrite($file,"            $getListName({"."\r\n");
        fwrite($file,"                page: index,"."\r\n");
        fwrite($file,"                status: this.status,"."\r\n");
        fwrite($file,"            }).then(res => {"."\r\n");
        fwrite($file,"                this.lists = res.data.lists;"."\r\n");
        fwrite($file,"                this.allpage = res.data.allpage;"."\r\n");
        fwrite($file,"            })"."\r\n");
        fwrite($file,"        },"."\r\n");

        //新增数据
        fwrite($file,"        showDialog: function () {"."\r\n");
        fwrite($file,"            this.model = {};"."\r\n");
        if(count($moreImageUoloadList) > 0){
            foreach ($moreImageUoloadList as $moreImageName){
                fwrite($file,"            this.$moreImageName = [];"."\r\n");
            }
        }
        if(count($onlyImageUoloadList) > 0){
            foreach ($onlyImageUoloadList as $onlyImageName){
                fwrite($file,"            this.$onlyImageName = '';"."\r\n");
            }
        }
        fwrite($file,"            this.dvEdit = true;"."\r\n");
        fwrite($file,"        },"."\r\n");

        //编辑数据
        fwrite($file,"        updateModel: function (row) {"."\r\n");
        fwrite($file,"            this.model = row;"."\r\n");
        if(count($moreImageUoloadList) > 0){
            foreach ($moreImageUoloadList as $moreImageName){
                fwrite($file,"            this.$moreImageName = [];"."\r\n");
                fwrite($file,"            for (let i = 0;i<row.$moreImageName.length;i++){"."\r\n");
                fwrite($file,"                let item = {"."\r\n");
                fwrite($file,"                    'url': row.$moreImageName".'[i],'."\r\n");
                fwrite($file,"                    'uid': Math.random(10000, 99999),"."\r\n");
                fwrite($file,"                }"."\r\n");
                fwrite($file,"                this.$moreImageName.push(item);"."\r\n");
                fwrite($file,"            };"."\r\n");
            }
        }
        if(count($onlyImageUoloadList) > 0){
            foreach ($onlyImageUoloadList as $onlyImageName){
                fwrite($file,"            this.$onlyImageName = row.$onlyImageName;"."\r\n");
            }
        }
        fwrite($file,"            this.dvEdit = true;"."\r\n");
        fwrite($file,"        },"."\r\n");

        //重置搜索条件
        fwrite($file,"        reset: function () {"."\r\n");
        fwrite($file,"            this.status = '';"."\r\n");
        fwrite($file,"        },"."\r\n");

        //条件搜索数据
        fwrite($file,"        search: function () {"."\r\n");
        fwrite($file,"            this.handleCurrentChange(1);"."\r\n");
        fwrite($file,"        },"."\r\n");

        //dialog关闭确认
        fwrite($file,"        handleClose: function (done) {"."\r\n");
        fwrite($file,'            this.$confirm("确认关闭？")'."\r\n");
        fwrite($file,'                .then(_ => {'."\r\n");
        fwrite($file,'                    done();'."\r\n");
        fwrite($file,'                })'."\r\n");
        fwrite($file,'                    .catch(_ => {'."\r\n");
        fwrite($file,'                });'."\r\n");
        fwrite($file,"        },"."\r\n");

        //显示和隐藏表格列
        fwrite($file,"        changeColumn(columns) {"."\r\n");
        fwrite($file,"            this.columns = columns;"."\r\n");
        fwrite($file,'            this.$refs.table.doLayout()'."\r\n");
        fwrite($file,"        },"."\r\n");


        //提交数据
        fwrite($file,"        onSubmit: function () {"."\r\n");
        fwrite($file,"            $editName({"."\r\n");
        fwrite($file,"                id: this.model.id,"."\r\n");
        fwrite($file,"                remark: this.model.remark,"."\r\n");
        fwrite($file,"                status: this.model.status,"."\r\n");
        foreach ($columnList as $col){
            if(!in_array($col['column'],['id','create_time','status','remark','update_time'])){
                if(strpos($col['column_remark'],'[富文本]') !== false){
                    fwrite($file,"                ".$col['column'].": this.model.".$col['column'].","."\r\n");
                }elseif(strpos($col['column_remark'],'[单图上传]') !== false){
                    fwrite($file,"                ".$col['column'].": this.".$col['column'].","."\r\n");
                }elseif(strpos($col['column_remark'],'[多图上传]') !== false){
                    fwrite($file,"                ".$col['column'].": this.".$col['column'].","."\r\n");
                }else{
                    if(strpos($col['column_remark'],'[标记字段]') === false) {
                        fwrite($file,"                ".$col['column'].": this.model.".$col['column'].","."\r\n");
                    }
                }
            }
        }
        fwrite($file,'            }).then(res => {'."\r\n");
        fwrite($file,"                if (res.code === 1) {"."\r\n");
        fwrite($file,'                    this.$message({'."\r\n");
        fwrite($file,"                        message: '操作成功',"."\r\n");
        fwrite($file,"                        type: 'success'"."\r\n");
        fwrite($file,'                    });'."\r\n");
        fwrite($file,'                    this.dvEdit = false;'."\r\n");
        fwrite($file,'                    this.handleCurrentChange(this.current);'."\r\n");
        fwrite($file,'                }'."\r\n");
        fwrite($file,'            })'."\r\n");
        fwrite($file,"        },"."\r\n");


        //删除数据
        fwrite($file,"       deleteModel: function (row) {"."\r\n");
        fwrite($file,'            this.$confirm("您确认要删除该条数据?", "提示", {'."\r\n");
        fwrite($file,"                confirmButtonText: '确定',"."\r\n");
        fwrite($file,"                cancelButtonText: '取消',"."\r\n");
        fwrite($file,"                type: 'info'"."\r\n");
        fwrite($file,'            }).then(() => {'."\r\n");
        fwrite($file,"                $deleteName({"."\r\n");
        fwrite($file,"                    id: row.id,"."\r\n");
        fwrite($file,'                }).then(res => {'."\r\n");
        fwrite($file,"                    if (res.code === 1) {"."\r\n");
        fwrite($file,'                        this.$message({'."\r\n");
        fwrite($file,"                            message: '操作成功',"."\r\n");
        fwrite($file,"                            type: 'success'"."\r\n");
        fwrite($file,'                        });'."\r\n");
        fwrite($file,'                        this.handleCurrentChange(this.current);'."\r\n");
        fwrite($file,'                    }'."\r\n");
        fwrite($file,'                })'."\r\n");
        fwrite($file,'            }).catch(() => {'."\r\n");
        fwrite($file,'            })'."\r\n");
        fwrite($file,"        },"."\r\n");


        //单张图片上传的相关函数
        if($isOnlyImageUpload){
            foreach ($onlyImageUoloadList as $key =>$onlyImageName){
                if($key == 0){
                    fwrite($file,"        handleAvatarSuccess: function (res, file) {"."\r\n");
                    fwrite($file,"            this.$onlyImageName = res.data.src;"."\r\n");
                    fwrite($file,"        },"."\r\n");
                    fwrite($file,"        handleRemove: function (file, fileList) {"."\r\n");
                    fwrite($file,"            this.$onlyImageName = '';"."\r\n");
                    fwrite($file,"        },"."\r\n");
                    fwrite($file,"        beforeRemove: function (file, fileList) {"."\r\n");
                    fwrite($file,"            this.$onlyImageName = '';"."\r\n");
                    fwrite($file,"        },"."\r\n");
                }else{
                    fwrite($file,"        handleAvatarSuccess".$key.": function (res, file) {"."\r\n");
                    fwrite($file,"            this.$onlyImageName = res.data.src;"."\r\n");
                    fwrite($file,"        },"."\r\n");
                    fwrite($file,"        handleRemove".$key.": function (file, fileList) {"."\r\n");
                    fwrite($file,"            this.$onlyImageName = '';"."\r\n");
                    fwrite($file,"        },"."\r\n");
                    fwrite($file,"        beforeRemove".$key.": function (file, fileList) {"."\r\n");
                    fwrite($file,"            this.$onlyImageName = '';"."\r\n");
                    fwrite($file,"        },"."\r\n");
                }
            }
        }

        //多张图片上传的相关函数
        if($isMoreImageUpload){
            foreach ($moreImageUoloadList as $key => $moreImageName){
                if($key == 0){
                    fwrite($file,"        moreHandleRemove: function (file,fileList) {"."\r\n");
                    fwrite($file,"            this.$moreImageName = fileList;"."\r\n");
                    fwrite($file,"        },"."\r\n");
                    fwrite($file,"        uploadFile:function(file){"."\r\n");
                    fwrite($file,"            var fm = new FormData();"."\r\n");
                    fwrite($file,"            fm.append('file', file.file);"."\r\n");
                    fwrite($file,"            axios.post('/uploadCommon/uploadImage',fm).then(res=>{"."\r\n");
                    fwrite($file,"                var arr = {"."\r\n");
                    fwrite($file,"                    'url': res.data.data.src,"."\r\n");
                    fwrite($file,"                    'uid': file.file.uid,"."\r\n");
                    fwrite($file,"                };"."\r\n");
                    fwrite($file,"                this.$moreImageName.push(arr);"."\r\n");
                    fwrite($file,"            })"."\r\n");
                    fwrite($file,"        },"."\r\n");
                }else{
                    fwrite($file,"        moreHandleRemove".$key.": function (file,fileList) {"."\r\n");
                    fwrite($file,"            this.$moreImageName = fileList;"."\r\n");
                    fwrite($file,"        },"."\r\n");
                    fwrite($file,"        uploadFile".$key.":function(file){"."\r\n");
                    fwrite($file,"            var fm = new FormData();"."\r\n");
                    fwrite($file,"            fm.append('file', file.file);"."\r\n");
                    fwrite($file,"            axios.post('/uploadCommon/uploadImage',fm).then(res=>{"."\r\n");
                    fwrite($file,"                var arr = {"."\r\n");
                    fwrite($file,"                    'url': res.data.data.src,"."\r\n");
                    fwrite($file,"                    'uid': file.file.uid,"."\r\n");
                    fwrite($file,"                };"."\r\n");
                    fwrite($file,"                this.$moreImageName.push(arr);"."\r\n");
                    fwrite($file,"            })"."\r\n");
                    fwrite($file,"        },"."\r\n");
                }
            }
        }

        fwrite($file,"    },"."\r\n");
        fwrite($file,"}"."\r\n");
        fwrite($file,"</script>"."\r\n");
        //方法定义结束

        fwrite($file,"\r\n");

        //style样式定义开始
        fwrite($file,"<style lang='scss' scoped>"."\r\n");

        fwrite($file,".page-content {"."\r\n");
        fwrite($file,"    width: 100%;"."\r\n");
        fwrite($file,"    height: 100%;"."\r\n");
        fwrite($file,"    .avatar {"."\r\n");
        fwrite($file,"        width: 50px;"."\r\n");
        fwrite($file,"        height: 50px;"."\r\n");
        fwrite($file,"        border-radius: 50%;"."\r\n");
        fwrite($file,"    }"."\r\n");
        fwrite($file,"}"."\r\n");
        fwrite($file,"\r\n");
        if($isMoreImageUpload || $isOnlyImageUpload){
            fwrite($file,"avatar-uploader .el-upload {"."\r\n");
            fwrite($file,"    border: 1px dashed #d9d9d9;"."\r\n");
            fwrite($file,"    border-radius: 6px;"."\r\n");
            fwrite($file,"    cursor: pointer;"."\r\n");
            fwrite($file,"    position: relative;"."\r\n");
            fwrite($file,"    overflow: hidden;"."\r\n");
            fwrite($file,"}"."\r\n");
            fwrite($file,"\r\n");
            fwrite($file,".avatar-uploader .el-upload:hover {"."\r\n");
            fwrite($file,"    border-color: #409EFF;"."\r\n");
            fwrite($file,"}"."\r\n");
            fwrite($file,"\r\n");
            fwrite($file,".avatar-uploader-icon {"."\r\n");
            fwrite($file,"    font-size: 28px;"."\r\n");
            fwrite($file,"    color: #8c939d;"."\r\n");
            fwrite($file,"    width: 178px;"."\r\n");
            fwrite($file,"    height: 178px;"."\r\n");
            fwrite($file,"    line-height: 178px;"."\r\n");
            fwrite($file,"    text-align: center;"."\r\n");
            fwrite($file,"}"."\r\n");
            fwrite($file,"\r\n");
            fwrite($file,".avatar {"."\r\n");
            fwrite($file,"    width: 178px;"."\r\n");
            fwrite($file,"    height: 178px;"."\r\n");
            fwrite($file,"    display: block;"."\r\n");
            fwrite($file,"}"."\r\n");
            fwrite($file,"\r\n");
            fwrite($file,"input[type='file'] {"."\r\n");
            fwrite($file,"    display: none;"."\r\n");
            fwrite($file,"}"."\r\n");
            fwrite($file,"\r\n");
        }

        fwrite($file,"</style>"."\r\n");
        //style样式定义结束
        fwrite($file,"\r\n");
        fclose($file);
        return true;
    }

    public static function getTableFileName($tableName){
        return camelize($tableName,'_',true);
    }

    /*
     * 去掉表的前缀
     */
    public static function cutTablePrefix($tableName,$table_prefix){
        return str_replace($table_prefix,'',$tableName);
    }

    /*
     * 正则匹配
     */
    public static function getPatternStr($str){
        preg_match('/【(.*)】/', $str, $matches);
        if(count($matches) > 0){
            return $matches[0];
        }else{
            return '';
        }
    }

}